class: title-slide, left, bottom # Automation with purrr ---- ## **or how we created over 3000 pdf letters!** ### Josephine Browning ### 11/11/2020 --- class: center,middle # About Me ## Josephine Browning ### Business Intelligence Manager at Gloucestershire CCG ---- .left[I have over 10 years experience as an NHS Analyst working across Public Health, Acute Providers and Commissioning Organisations] .left[I am an NHS-R community trainer and an AphA professionally registered analyst] .left[I am also currently studying for an MSc in Health Data Science at Swansea University] ---- [
@randomrainbow86](https://twitter.com/randomrainbow86)<br/> [
josephine.browning@nhs.net](mailto:josephine.browning@nhs.net)<br/> 🎉 --- class: inverse, middle, center # Why write 3000 letters? --- # Shielded Patients Welfare Calls ---- ### In Gloucestershire we had over 10,000 patients that were identified as requiring to shield -- ### To help some of our GP's we set up a call centre to contact all of the identified patients for a welfare check -- ### Patients were asked a series of questions to ensure they understood what shileding was, why they were being asked to do it and if they needed any help to allow them to shield -- ### Following each call a transcript of the conversation was required to be sent to the GP practice to be added into the patients primary care record -- ### That was where we came in... how do we write all of those PDF's without having to compile each one separately? --- # Using R Markdown to create a template ---- ### The first task was to create a letter template that would be populated for each patient -- ### Using R Markdown this was written to output to a pdf file format -- ### Utilised the inline code function to input the answers to each question -- ### Utilised the Params functionality to input each patients details --- #Inserting an SQL - R link ---- ###To help with the automation the data was pulled into R directly from the SQL data warehouse -- ###This was done individually within the R Markdown template for each patient (This could probably have been coded better to bring all the data in at the start) -- ###We also used this link code to create a dataframe to run the Purrr function over --- #Using Purrr ---- ###The [Purrr](purrr.tidyverse.org) package is part of the tidyverse and has a quite a few useful functions -- ###Map can be used as a nice tidy way to code for loops -- ###For this task we used the Walk2 function as we did not need to see the outputs as it was running --- #Putting it all together .panelset[ .panel[.panel-name[Bullets] ###The R-Markdown Template needed an NHS number to produce the letter for that patient ###The Walk2 function needed a dataframe of NHS numbers and GP practice codes and also a function to iterate over ###The output was that a PDF letter was created for each NHS number and this was saved down into a separate folder for each GP practice ] .panel[.panel-name[Render Report] ```r render_report <- function(NHS_Number, Practice) { filename <- paste0("Sample/folder/",Practice,"/",NHS_Number,".pdf") render(input = "Script1.Rmd", output_format = "pdf_document", output_file = filename, params = list(NHS_Number = NHS_Number)) } ``` ###The above code is used to run/render the R Markdown letter template for a given NHS Number] .panel[.panel-name[Walk2] ```r library(rmarkdown) library(purrr) walk2 (.x = data$NHS_Number, .y = data$Practice_Code, .f = render_report) ``` ###The above code produced a PDF for the each NHS number in the dataframe saved in a file for each practice code ] ] --- class: center, middle #With thanks to: ##Anna Schorfield and Lee Tarbuck